package com.movitech.mobile.cz.base.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.common.util.DateUtils;
import com.movitech.mobile.cz.base.sys.entity.SAttachment;
import com.movitech.mobile.cz.base.sys.entity.ext.ExtSAttachment;
import com.movitech.mobile.cz.base.sys.entity.ext.ExtSAttachmentReq;
import com.movitech.mobile.cz.base.sys.mapper.SAttachmentMapper;
import com.movitech.mobile.cz.base.sys.mapper.ext.ExtSAttachmentMapper;
import com.movitech.mobile.cz.base.sys.service.SAttachmentService;
import com.movitech.mobile.cz.base.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 附件 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-03-01
 */
@Service("sAttachmentService")
@Transactional
public class SAttachmentServiceImpl extends ServiceImpl<SAttachmentMapper, SAttachment> implements SAttachmentService {

    @Autowired
    private SAttachmentMapper sAttachmentMapper;

    @Autowired
    private ExtSAttachmentMapper extSAttachmentMapper;

    @Override
    public SAttachment getByAttachmentPath(String attachmentPath) {
        List<SAttachment> sAttachments = sAttachmentMapper.getByAttachmentPath(attachmentPath);
        if(sAttachments != null && sAttachments.size() > 0){
            return sAttachments.get(0);
        }
        return null;
    }

    @Override
    public SAttachment getOneByEntity(SAttachment sAttachment) {
        EntityWrapper entityWrapper = new EntityWrapper();
        sAttachment.setDelFlag(CommonConstants.NO_INTEGER);
        entityWrapper.setEntity(sAttachment);
        return selectOne(entityWrapper);
    }

    /**
     *
     *
     * @param formId
     * @param type
     * @param subType 可以为null
     * @return
     */
    @Override
    public List<SAttachment> getByFormIdAndAttachmentType(String formId, Integer type,Integer subType) {
        EntityWrapper entityWrapper = new EntityWrapper();
        SAttachment sAttachment = new SAttachment();
        sAttachment.setFormId(formId);
        sAttachment.setAttachmentType(type);
        if(subType!=null){
            sAttachment.setAttachmentSubType(subType);
        }
        sAttachment.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
        entityWrapper.setEntity(sAttachment);
        return selectList(entityWrapper);
    }
    
    /**
    *
    *
    * @param formId
    * @param type
    * @param subType 可以为null
    * @return
    */
   @Override
   public List<SAttachment> getExtByFormIdAndAttachmentType(String formId, Integer type,Integer subType) {
       SAttachment where = new SAttachment();
       where.setFormId(formId);
       where.setAttachmentType(type);
       where.setAttachmentSubType(subType);
       where.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
       EntityWrapper entityWrapper = new EntityWrapper();
       entityWrapper.setEntity(where);


       return selectList(entityWrapper);
   }


    @Override
    public List<SAttachment> getByFormIdOrderByType(String formId) {
        EntityWrapper entityWrapper = new EntityWrapper();
        SAttachment sAttachment = new SAttachment();
        sAttachment.setFormId(formId);
        sAttachment.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
        entityWrapper.setEntity(sAttachment);
        entityWrapper.orderBy("attachment_type");
        return selectList(entityWrapper);
    }

    @Override
    public List<ExtSAttachment> getExtByEntity(ExtSAttachmentReq extSAttachmentReq) {
        return extSAttachmentMapper.selectExtByEntity(extSAttachmentReq);
    }

    @Override
    public void updateSendTime(SAttachment where) {
        where.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
        Date now = DateUtils.getNow();
        SAttachment sAttachment = new SAttachment();
        sAttachment.setSendTime(now);
        sAttachment.setUpdateBy(UserUtils.getCurrentUserId());
        sAttachment.setUpdateDate(now);
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.setEntity(where);
        update(sAttachment,entityWrapper);
    }

	@Override
	public int deleteBatch(String relatedId, int attachmentType, int attachmentSubType) {
		return extSAttachmentMapper.deleteBatch(relatedId, attachmentType, attachmentSubType);
	}

    @Override
    public boolean deleteById(String id) {
        SAttachment sAttachment = new SAttachment();
        sAttachment.setId(id);
        sAttachment.setUpdateBy(UserUtils.getCurrentUserId());
        sAttachment.setUpdateDate(DateUtils.getNow());
        sAttachment.setDelFlag(CommonConstants.DEL_FLAG_DELETE);
        return updateById(sAttachment);
    }

    @Override
	public List<ExtSAttachment> getExtByRelatedIdAndTypeAndSubType(String relatedId, Integer type, Integer subType) {
		return extSAttachmentMapper.getExtByRelatedIdAndTypeAndSubType(relatedId, type, subType);
	}

    @Override
    public List<ExtSAttachment> getSAttachmentByCnd(Map<String, Object> mapParams) {
        return extSAttachmentMapper.getSAttachmentByCnd(mapParams);
    }
}
